<template>
	<view class="take-order full box relative bg-primary flex-col">
		<view class="w-wrap">
			<view class="w-inner">
				<scroll-view scroll-y="true" class="full" @scrolltolower="handleLoadMore">
					<view class="bottom-box">
						<u-form label-width="220rpx" :model="formData" :rules="formRules" ref="formRef">
							<u-form-item label="联系人姓名:" prop="name">
								<u-input v-model="formData.name" placeholder="请输入联系人姓名" />
							</u-form-item>
							<u-form-item label="联系人电话:" prop="mobile">
								<u-input v-model="formData.mobile" placeholder="请输入联系人电话" />
							</u-form-item>
							<!-- <u-form-item label="安装地址:" prop="address">
								<u-input v-model="formData.address" placeholder="请选择安装地址" type="select" />
							</u-form-item> -->
							<u-form-item label="详细地址:" prop="shouhuo_address">
								<u-input v-model="formData.shouhuo_address" placeholder="请输入详细地址" />
							</u-form-item>
							<u-form-item label="选择邀请设备:" prop="shebei_id">
								<u-input @click="handleChooseDevice" v-model="formData.shebei_name"
									placeholder="请选择选择邀请设备" type="select" />
							</u-form-item>
							<u-form-item label="备注:" prop="content">
								<u-input v-model="formData.content" placeholder="请输入备注" />
							</u-form-item>
							<u-form-item label="填写设备号:" prop="shebei_bianma">
								<u-input v-model="formData.shebei_bianma" placeholder="请输入设备号" />
							</u-form-item>
						</u-form>
					</view>
					<view class="record-item-wrap" v-for="(item,index) in list" :key="index">
						<view class="record-item">
							<view class="checkbox-item" @click="handleCheckItem(item)"
								:class="[item.id === activeDeviceId ? 'active' : '']">
								<u-icon color="#ffffff" size="34rpx" name="checkmark" />
							</view>
							<image :src="item.image" class="item-image" mode="aspectFill"></image>
							<view class="item-info">
								<text class="pack-name">{{ item.name }}</text>
								<view class="first-price">
									<text>价格：</text>
									<text class="is-active">¥{{ item.pay_price || "" }}</text>
								</view>
								<!-- <view class="wifi-price is-active">
								<text>流量费：</text>
								<text class="is-active">¥180.00</text>
							</view>
							<view class="wifi-price">
								<text>流量代缴价：</text>
								<text class="is-active">¥180.00</text>
							</view> -->
								<view class="pack-content">
									<text class="common-text">套餐内容：</text>
									<text class="active-text">{{ item.taocan_neirong || "" }}</text>
									<!-- <text class="active-text">[0.28元/升]</text> -->
								</view>
								<text class="desc-item">时限：{{ item.day_num || "" }}天</text>
								<!-- <view class="item-remark">
								<text>{{ item.taocan_shuoming || ''}}</text>
							</view> -->
							</view>
						</view>
						<view class="record-footer">
							<text class="footet-label">是否从备货库存出货:</text>
							<view class="check-box-item active" @click.stop="handleChangeFrom(item,1)">
								<view class="checkbox-item is-small" :class="[item.from_kucun === 1 ? 'active' : '']">
									<u-icon color="#ffffff" size="28rpx" name="checkmark" />
								</view>
								<text class="checkbx-label">是</text>
							</view>
							<view class="check-box-item" @click.stop="handleChangeFrom(item,0)">
								<view class="checkbox-item is-small" :class="[item.from_kucun === 0 ? 'active' : '']">
									<u-icon color="#ffffff" size="28rpx" name="checkmark" />
								</view>
								<text class="checkbx-label">否</text>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<view class="page-footer">
			<view class="total-price">
				<text>合计：</text>
				<text v-if="is_shiyong > 0" class="total-value">￥0</text>
				<template v-else>
					<text v-if="activeTarget" class="total-value">￥{{ activeTarget.pay_price || ""}}</text>
				</template>
			</view>
			<view class="confirm-btn" @click="handleConfirm">
				<text>立即代付</text>
			</view>
		</view>
		<choose-device ref="chooseDeviceRef" @success="handleChooseSuccess" />
	</view>
</template>

<script>
	import ChooseDevice from "@/pages/index/take-order/chooseDevice.vue"
	import { request, toast, showLoading } from "@/utils/tool"
	export default {
		components: { ChooseDevice },
		data() {
			return {
				is_shiyong: 0,
				seconds: 10,
				activeDeviceId: "",
				formData: {
					name: "",
					mobile: "",
					content: "",
					shouhuo_address: "",
					guanjia_user_id: 0,
					shebei_id: "",
					shebei_name: "",
					shebei_bianma: "",
				},
				list: [],
				codeText: "",
				addressValue: "",
				hasMore: true,
				page: 1,
				size: 15,
			}
		},
		computed: {
			activeTarget() {
				return this.list.find(item => item.id === this.activeDeviceId)
			},
			formRules() {
				return {
					name: [{
						required: true,
						message: '请输入加盟人姓名',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['change', 'blur'],
					}],
					mobile: [{
						required: true,
						message: '请输入加盟人电话',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['change', 'blur'],
					}],
					shouhuo_address: [{
						required: true,
						message: '请输入详细地址',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['change', 'blur'],
					}],
				}
			}
		},
		onReady() {
			this.$refs.formRef.setRules(this.formRules)
		},
		created() {
			this.initData()
		},
		methods: {
			handleToInvite() {
				uni.navigateTo({
					url: "/pages/index/take-order/poster"
				})
			},
			handleChooseSuccess(ev) {
				const { name, id } = ev
				this.formData.shebei_id = id
				this.formData.shebei_name = name
			},
			handleChooseDevice() {
				this.$refs.chooseDeviceRef.open(this.formData.shebei_id)
			},
			async handleConfirm() {
				this.$refs.formRef.validate(async flag => {
					if (!flag) return
					const { activeTarget } = this
					if (!activeTarget) return toast({ title: "请选择设备" })
					const { name, content, mobile, shouhuo_address, shebei_bianma, shebei_id } = this
						.formData
					const { is_shiyong } = this
					const hide = showLoading("正在提交")
					const resp = await request({
						url: "/tihuo/tihuosubmit",
						data: {
							is_shiyong,
							name,
							mobile,
							content,
							recharge_config_id: activeTarget.id,
							from_kucun: activeTarget.from_kucun,
							shouhuo_address,
							shebei_id,
							shebei_bianma
						}
					}).finally(hide)
					if (resp.code !== 1) {
						return toast({ title: "提交失败" })
					}
					resp.data.order_amount = activeTarget.pay_price
					uni.navigateTo({
						url: "/pages/pay/index?data=" + JSON.stringify(resp.data)
					})
				})

			},
			handleCheckItem(rowItem) {
				this.activeDeviceId = rowItem.id
			},
			handleChangeFrom(rowItem, value) {
				rowItem.from_kucun = value
			},
			handleLoadMore() {
				if (!this.hasMore) return
				this.page += 1
				this.initData()
			},
			async initData() {
				const hide = showLoading("正在获取")
				const { page, size } = this
				const resp = await request({
					url: "/tihuo/tihuolists",
					method: "GET",
					data: {
						page_no: page,
						page_size: size
					}
				}).finally(hide)
				console.log(resp, 'resp');
				const { lists, count, page_no, page_size } = resp.data
				if (page === 1) this.list = []
				this.list.push(...lists.map(item => {
					item.from_kucun = 0
					return item
				}))
				this.hasMore = page_no * page_size < count
			},
			handleCodeChange(text) {
				this.codeText = text;
			},
			handleOnCodeEnd() {
				console.log('end');
			},
			handleOnCodeStart() {
				console.log('start');
			}
		},
		onLoad(ev) {
			if (ev.is_shiyong) {
				this.is_shiyong = 1
        uni.setNavigationBarTitle({
          title:"试用开单"
        })
			}
			if (ev.guanjia_user_id) {
				this.formData.guanjia_user_id = ev.guanjia_user_id
			}
		}
	}
</script>

<style lang="scss" scoped>
	.take-order {
		padding: 28rpx 20rpx 0;
		box-sizing: border-box;

		.page-footer {
			padding: 32rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			background: #fff;

			.confirm-btn {
				width: 320rpx;
				height: 80rpx;
				text-align: center;
				line-height: 80rpx;
				background: linear-gradient(90deg, #7AA4E8 0%, #0A67FD 100%);
				border-radius: 100rpx 100rpx 100rpx 100rpx;
				font-weight: bold;
				font-size: 28rpx;
				color: #FFFFFF;

				&:active {
					opacity: .7;
				}
			}

			.total-price {
				font-size: 28rpx;
				color: rgba(0, 0, 0, 0.9);
				line-height: 44rpx;

				.total-value {
					color: #FF4A18;
					font-weight: bold;
					font-size: 34rpx;
				}
			}
		}


		.record-item-wrap {
			padding: 32rpx 22rpx 32rpx;
			background: #fff;
			margin-top: 20rpx;

			.checkbox-item {
				width: 56rpx;
				height: 56rpx;
				flex-shrink: 0;
				border: 2rpx solid #809CF6;
				border-radius: 50%;
				overflow: hidden;
				display: flex;
				align-items: center;
				justify-content: center;
				flex-shrink: 0;

				&.is-small {
					width: 44rpx;
					height: 44rpx;
				}

				&.active {
					background-color: #3361F0;
					border-color: #3361F0;
				}
			}

			.record-footer {
				margin-top: 24rpx;
				padding: 0 16rpx;
				display: flex;
				align-items: center;

				.check-box-item {
					display: flex;
					align-items: center;
					margin-left: 24rpx;

					&.active {
						margin-left: auto;
					}

					.checkbx-label {
						font-size: 24rpx;
						color: #444142;
						line-height: 44rpx;
						margin-left: 8rpx;
					}
				}

				.footet-label {
					font-size: 24rpx;
					color: #484848;
					line-height: 44rpx;
				}
			}

			.record-item {

				display: flex;
				align-items: center;



				.item-info {
					margin-left: 10rpx;
					display: flex;
					flex-flow: column;

					.item-remark {
						font-size: 24rpx;
						color: #515151;
						line-height: 28rpx;
						word-break: break-all;
						white-space: pre-wrap;
						margin-top: 4rpx;
					}

					.desc-item {
						font-size: 22rpx;
						color: #737373;
						line-height: 44rpx;
					}

					.item-price {
						font-size: 26rpx;
						color: #737373;
						line-height: 44rpx;

						.price-value {
							color: #FF0000;
						}
					}

					.pack-content {
						font-size: 24rpx;
						color: #515151;
						line-height: 44rpx;
						color: #737373;
					}

					.wifi-price {
						font-size: 26rpx;
						color: #515151;
						line-height: 44rpx;
						margin-top: 4rpx;

						&.is-active {
							text-decoration: line-through;
						}

						.is-active {
							color: #FF0000;
						}
					}

					.first-price {
						font-size: 26rpx;
						color: #515151;
						line-height: 44rpx;
						margin-top: 4rpx;

						.is-active {
							color: #FF0000;
						}
					}

					.pack-name {
						font-size: 28rpx;
						color: #515151;
						line-height: 44rpx;
					}
				}

				.item-image {
					width: 160rpx;
					height: 177rpx;
					flex-shrink: 0;
					margin-left: 14rpx;
				}
			}
		}

		.bottom-box {
			margin-top: 24rpx;
			padding: 8rpx 22rpx;
			background: #FFFFFF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			box-sizing: border-box;
			position: relative;
		}

		.form-box {
			margin-top: 24rpx;
			padding: 32rpx 22rpx;
			background: #FFFFFF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			box-sizing: border-box;
			position: relative;

			.get-code-btn {
				width: 138rpx;
				height: 46rpx;
				line-height: 46rpx;
				text-align: center;
				font-size: 20rpx;
				color: #FFFFFF;
				background: linear-gradient(90deg, #85AAE6 0%, #0967FE 100%);
				border-radius: 23rpx 23rpx 23rpx 23rpx;
			}

			.confirm-btn {
				margin: 44rpx auto 0;
				width: 437rpx;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				font-size: 36rpx;
				color: #FFFFFF;
				background: linear-gradient(90deg, #7FA7E7 0%, #0A67FD 100%);
				border-radius: 38rpx 38rpx 38rpx 38rpx;
			}

			.form-text {
				font-size: 28rpx;
				color: #464655;
				line-height: 40rpx;
			}

			.address-btn {
				margin-top: 20rpx;
				display: flex;
				align-items: center;
				justify-content: flex-end;

				uni-button {
					margin: 0;

					&:first-child {
						margin-right: 20rpx;
					}
				}

				button {
					margin: 0;

					&:first-child {
						margin-right: 20rpx;
					}
				}
			}

			.address-box {
				width: 668rpx;
				height: 168rpx;
				background: #EFEFEF;
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				font-size: 28rpx;
				line-height: 40rpx;
				padding: 20rpx;
				margin-top: 20rpx;
			}
		}

		.top-box {
			padding: 34rpx 22rpx;
			background: #FFFFFF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;

			.top-btn {
				width: 438rpx;
				height: 80rpx;
				font-size: 36rpx;
				color: #FFFFFF;
				line-height: 80rpx;
				margin: 36rpx auto 0;
				background: linear-gradient(90deg, #7FA7E7 0%, #0A67FD 100%);
				border-radius: 38rpx 38rpx 38rpx 38rpx;
				text-align: center;
			}

			.top-text {
				font-weight: 400;
				font-size: 28rpx;
				color: #464655;
				line-height: 40rpx;
				word-break: break-all;
				white-space: pre-wrap;
			}
		}
	}
</style>